package com.mobile.challenge.ui

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.mobile.challenge.api.ResetPasswordService
import com.mobile.challenge.bean.UserBean
import com.mobile.challenge.exception.MobileException
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

/**
 * created by  shakespace
 * 2020/9/18  1:59
 */
class ResetPasswordViewModel
@Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {

    var resetError: MutableLiveData<MobileException> = MutableLiveData()
        internal set

    var resetData: MutableLiveData<UserBean> = MutableLiveData()
        internal set

    fun reset(name: String, old: String, newPwd: String) {
        val disposable = getRetrofitService(ResetPasswordService::class.java).reset(name, old, newPwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        resetData.value = response.data
                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                        resetError.value = exception
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "9999"
                    exception.errorMsg = it.message
                    exception.hintMsg = "please try again later "
                    resetError.value = exception
                })
    }
}